﻿using System;
using System.Windows.Forms;
using System.Xml;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    

    public class A_InsertDocument : A_Action
    {
        public override string ActionName()
        {
            return "insertdocument";
        }

        public override bool Execute()
        {
            ET_Document myRecord = new ET_Document();
            myRecord.ObjectID = base.Param1;
            if (myRecord.isKeyEnable() && base.myOwnerDocument.DataSource.DBConn.ReadOneRecord(myRecord))
            {
                using (ZYTextDocument document2 = new ZYTextDocument())
                {
                    base.myOwnerDocument.BeginContentChangeLog();
                    base.myOwnerDocument.BeginUpdate();
                    document2.DataSource.DBConn = base.myOwnerDocument.DataSource.DBConn;
                    document2.FromXML(myRecord.GetDataXML().DocumentElement);
                    document2.DataSource.DBConn = null;
                    base.myOwnerDocument.EndUpdate();
                    base.myOwnerDocument.EndContentChangeLog();
                    return true;
                }
            }
            return false;
        }

        public override bool isEnable()
        {
            return base.myOwnerDocument.CanModify();
        }

        public override bool UIExecute()
        {
            //using (frmDBFileList list = new frmDBFileList())
            //{
            //    list.DBConn = base.myOwnerDocument.DataSource.DBConn;
            //    list.RefreshList();
            //    if (list.ShowDialog() == DialogResult.OK)
            //    {
            //        ET_Document selectedRecord = list.SelectedRecord;
            //        if (base.myOwnerDocument.DataSource.DBConn.ReadOneRecord(selectedRecord))
            //        {
            //            try
            //            {
            //                XmlDocument dataXML = selectedRecord.GetDataXML();
            //                using (ZYTextDocument document3 = new ZYTextDocument())
            //                {
            //                    document3.DataSource.DBConn = base.myOwnerDocument.DataSource.DBConn;
            //                    document3.FromXML(dataXML.DocumentElement);
            //                    document3.DataSource.DBConn = null;
            //                    return true;
            //                }
            //            }
            //            catch (Exception exception)
            //            {
            //                MessageBox.Show(exception.ToString());
            //            }
            //        }
            //    }
            //}
            return false;
        }
    }
}

